import csv 
with open("data.csv",mode='r',encoding="utf-8") as f:
    Read=csv.DictReader (f)
    for line in Read:
        print(line)


# data = [
#     {'Name':'Alice', 'Age':'30', 'City':'New York'},
#     {'Name':'Bob', 'Age':'25', 'City':'Los Angeles'}
# ]

# with open("output.csv","w",encoding="utf-8",newline="") as file:
#     fieldnames = ['Name', 'Age', 'City']
#     csv_dict_writer = csv.DictWriter(file, fieldnames=fieldnames)


#     csv_dict_writer.writeheader() 

#     for row in data:
#         csv_dict_writer.writerow(row)

data = [
    {'Name': 'Alice', 'Age': '30', 'City': 'New York'},
    {'Name': 'Bob', 'Age': '25', 'City': 'Los Angeles'}
]

with open('output.csv', mode='w', encoding='utf-8', newline='') as file:
    fieldnames = ['Name', 'Age', 'City']
    csv_dict_writer = csv.DictWriter(file, fieldnames=fieldnames)
    
    # 写入表头
    csv_dict_writer.writeheader()
    
    # 写入数据
    for row in data:
        csv_dict_writer.writerow(row)